<div class="centered">
<?php

function performance_string($p)
{
	$res = "";
	
	list($W, $L, $N) = Array($p->player_points, $p->player_games - $p->player_points, $p->player_games);
	if ($N == 0)
	{
		if ($p->player_end_rating > 0)
			return $p->player_end_rating;
		return "N/A";
	}
   	$rating = floor($p->player_avg_opp_rating + (400*($W - $L))/$N);
   	$res = "$rating = " . floor($p->player_avg_opp_rating) . " + (400 * ($W - $L)) / $N";
   	return $res;
}

function print_header(&$log)
{
	global $tdict;
	global $player;
	$tid = $log->tournament_id;
	$t = $tdict[$tid];?>
	<a class="link" href='tournament.php?id=<?php echo $tid ?>'>
	<?php echo $t->tournament_name?></a>
	<br />
	<?php
		if ($t->start_date == $t->end_date)
			echo  "<small>$t->start_date</small>";
		else
			echo "<small>$t->start_date -> $t->end_date</small>" 
	?>
	<br />
	<?php if ($log->player_start_rating < 0 && $log->player_end_rating > 0) {?>
		Rating: <?php echo performance_string($log)?>
	<?php }else if ($log->player_end_rating > 0){?>
		Rating: <?php echo "$log->player_start_rating -> $log->player_end_rating"?>
	<?php }else{?>
		Provisional: <?php echo performance_string($log)?>
	<?php } //endif?>
	<br />
<?php 
} 

function get_div_class(&$log)
{
	global $tdict;
	$tid = $log->tournament_id;
	$t = $tdict[$tid];
	if ($t->tournament_id < 0)
		return 'event';
	if (!($t->is_rated))
		return 'unrated';
	if ($log->player_start_rating < 0 && $log->player_end_rating > 0)
		return 'newrating';
	return 'usual';
}

function get_rating(&$log)
{
	if ($log->oppestablished)
		return floor($log->opprating);
	return "&nbsp;";
}

function get_rating_diff(&$log)
{
	if (!$log->oppestablished || !$log->meestablished)
		return "-";
	$rating_diff = sprintf("%.2f", $log->k_factor*($log->actual_score - $log->expected_score));
	if ($rating_diff > 0)
		return "+" . $rating_diff;
	return $rating_diff;
}

function get_result(&$log)
{
	$as = $log->actual_score - 0.5;
	if ($as > 0)
		return '<div class="win">win</div>';
	else if ($as < 0)
		return '<div class="loss">loss</div>';
	return '<div class="draw">draw</div>';
}

function get_opponent(&$log)
{
	global $pdict;
	global $player;
	static $ref_id = 0;
	$ref_id++;
	$pid = $log->opponent_id;
	$text = $pdict[$pid]->surname . "&nbsp;" . $pdict[$pid]->name; 
	return "<a onmouseover='get_score($ref_id, $player->player_id, $log->opponent_id)' id='t$ref_id' 
		class='smalllink2 qtip' href='player.php?id=$pid'>$text</a>";	
}

foreach ($trns as $trn){?>
	<div class="<?php $div_class = get_div_class($trn[0]); echo $div_class ?>">	
		<?php print_header($trn[0]);?>
		<?php if ($div_class != "event") {?>
			<table class='playertable'>
			<tr>
			<th>Opponent</th>
			<th>Rating</th>
			<th>Round</th>
			<th>Result</th>
			<th>R. Change</th>
			</tr>
			<?php foreach ($trn as $log){
				if (!isset($first))
					$first = $log; 
			?>
			<tr>
				<td><?php echo get_opponent($log)?></td>
				<td><?php echo get_rating($log)?></td>
				<td><span title="<?php echo $log->game_id ?>"><?php echo $log->round?></span></td>
				<td><?php echo get_result($log)?></td>
				<td><?php echo get_rating_diff($log)?></td>
			</tr>
			<?php } //foreach	?>
			</table>
		<?php } elseif (!isset($first)) $first = $trn[0] // if?>
	</div>
<?php } //foreach ?>

</div>

<script type="text/javascript">
init_tooltip();
</script>
